#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

##################
# MODULE STARTUP
##################

import mh_python, random

mh_python.initbrain();

counter = 1

##################
# MODULE EVENT HANDLERS
##################
# def on_eventname(connection, event, bot)

def on_pubmsg(connection, event, bot):
	my_nick = connection.get_nickname()
	msg = " "+event.arguments()[0].strip()
	msg = msg.strip()
	if (msg.find(my_nick) != -1) or (msg.find(my_nick.lower()) != -1) or (msg.find("~") == 0):
		if (random.randint(1,10) == 5): msg = msg.replace(my_nick,'')
		bot.sendMsg(event.target(),mh_python.doreply(msg))
	elif speak():
		bot.sendMsg(event.target(),mh_python.doreply(msg))
	else: 
		bot.log.debug("learning msg: %s" % (event.arguments()[0]))
		mh_python.learn(msg)
	if kick() and not (event.source().split('!')[0].find('etto') != -1): connection.kick(event.target(), event.source().split('!')[0], mh_python.doreply(msg))
	if save(): bot.log.debug('brain saved')


##################
# MODULE FUNCTIONS
##################

def kick():
	if random.randint(0,100000) > 99997:
		return True
	else: return False

def speak():
	if random.randint(0,2000) > 1995:
		return True
	else: return False

def save():
	global counter
	if counter > 50:
		mh_python.cleanup()
		counter = 0
		return True
	else: 
		counter = counter + 1
		return False